﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using BUS;
using DTO;
using System.Xml.Linq;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace GiaoDien
{
    public partial class ExportSach : Form
    {
        private DataSet dataset;
       // private string strquery;
        public ExportSach()
        {
            InitializeComponent();
        }
        private void init()
        {
            dataset = new DataSet();
            DataTable datatabletls = new DataTable();
            dataset.Tables.Add(datatabletls);
            dataset.Tables[0].TableName = "theloaisach";
            DataTable datatabletg = new DataTable();
            dataset.Tables.Add(datatabletg);
            dataset.Tables[1].TableName = "tacgia";
            DataTable datatablenxb = new DataTable();
            dataset.Tables.Add(datatablenxb);
            dataset.Tables[2].TableName = "nhaxuatban";
            DataTable datatablenn = new DataTable();
            dataset.Tables.Add(datatablenn);
            dataset.Tables[3].TableName = "ngonngu";
            DataTable datatables = new DataTable();
            dataset.Tables.Add(datatables);
            dataset.Tables[4].TableName = "sach";
        }
        private void Loadtheloai()
        {
            THELOAISACHBUS tlBUS = new THELOAISACHBUS();
            DataTable data = tlBUS.Loadtheloaisach();
            dataset.Tables["theloaisach"].Clear();
            dataset.Tables["theloaisach"].Merge(data);
        }
        private void Loadtacgia()
        {
            TACGIABUS tgBUS = new TACGIABUS();
            DataTable data = tgBUS.Loadtacgia();
            dataset.Tables["tacgia"].Clear();
            dataset.Tables["tacgia"].Merge(data);           
        }
        private void Loadnhaxuatban()
        {
            NHAXUATBANBUS nnxbBUS = new NHAXUATBANBUS();
            DataTable data = nnxbBUS.Loadnhaxuatban();
            dataset.Tables["nhaxuatban"].Clear();
            dataset.Tables["nhaxuatban"].Merge(data);            
        }

        private void Loadngongu()
        {
            NGONNGUBUS nnBUS = new NGONNGUBUS();
            DataTable data = nnBUS.Bangngonngu();
            dataset.Tables["ngonngu"].Clear();
            dataset.Tables["ngonngu"].Merge(data);         
        }
        private void Loadsach()
        {
            SACHBUS sBUS = new SACHBUS();
            DataTable data = sBUS.Bangsach();
            dataset.Tables["sach"].Clear();
            dataset.Tables["sach"].Merge(data);           
        }
        private void xemtatca()
        {
            Loadsach();
           // Loadtheloai();
            //Loadtacgia();
            //Loadnhaxuatban();
            //Loadngongu();
            dataGridView1.DataSource = dataset.Tables["sach"];
            //girdtacgia.DataSource = dataset.Tables["tacgia"];
            //girdtacgia.DisplayMember = "TenTacGia";
            //girdtacgia.ValueMember = "MaTacGia";
            //girdtheloai.DataSource = dataset.Tables["theloaisach"];
           // girdtheloai.DisplayMember = "TenTheLoai";
            //girdtheloai.ValueMember = "MaLoaiSach";
            //girdnhaxuatban.DataSource = dataset.Tables["nhaxuatban"];
            //girdnhaxuatban.DisplayMember = "TenNhaXuatBan";
            //girdnhaxuatban.ValueMember = "MaNhaXuatBan";
            //girdngonngu.DataSource = dataset.Tables["ngonngu"];
            //girdngonngu.DisplayMember = "TenNgonNgu";
            //girdngonngu.ValueMember = "MaNgonNgu";           
        }
        private void ExportSach_Load(object sender, EventArgs e)
        {
            try
            {
                init();
                
               
                xemtatca();
               

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnexport_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "Excel file *.xls|*.xls";
            if (save.ShowDialog() == DialogResult.OK)
            {

                Excel.Application xlApp = new Excel.Application();
                Excel.Workbook xlWorkBook;
                Excel.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;
                xlWorkBook = xlApp.Workbooks.Add(misValue);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                int i = 0;
                int j = 0;

                for (i = 0; i <= dataGridView1.RowCount - 1; i++)
                {
                    for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
                    {

                        DataGridViewCell cell = dataGridView1[j, i];
                        xlWorkSheet.Cells[1, j + 1] = dataGridView1.Columns[j].HeaderText;

                        xlWorkSheet.Cells[i + 2, j + 1] = cell.Value;

                    }
                }

                xlWorkBook.SaveAs(save.FileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();

                releaseObject(xlWorkSheet);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);

                MessageBox.Show("Excel file created ");
            }
        }
        private void releaseObject(object obj)
        {
            try
            {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                    obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
        }     
    }
}
